//
//  CustomNewTableViewCell.m
//  10-CustomTableViewCell
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CustomNewTableViewCell.h"
#import "UIView+Frame.h"
//使用@class生命的类，需要在。m中去import
#import "NewsObject.h"
@interface CustomNewTableViewCell ()
//不要和系统控件重名
@property (nonatomic, strong)UIImageView *newsImageView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *detailLabel;
@property (nonatomic,strong)UILabel *numberCountsLabel;
@end
@implementation CustomNewTableViewCell
#pragma mark - initWithStyle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    _newsImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
//        _newsImageView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_newsImageView];
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.font = [UIFont systemFontOfSize:20];
//        _titleLabel.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:_titleLabel];
    _detailLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _detailLabel.numberOfLines = 0;
//        _detailLabel.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:_detailLabel];
    _numberCountsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//        label上不要加视图
    [_detailLabel addSubview:_numberCountsLabel];
//        _numberCountsLabel.backgroundColor = [UIColor cyanColor];
    }
    
    return self;
}
// 有内容／坐标改变时，会触发该方法
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat leftMargin = 5;
    CGFloat topMargin = 5;
    CGFloat bottomMargin = 5;
    CGFloat middleMargin = 5;
    CGFloat rightMargin = 5;
    CGFloat titleLabelHeight = 30;
//    CGFloat middleOftitleLableAndDetailLabel = 5;
    _newsImageView.frame = CGRectMake(leftMargin, topMargin, self.contentView.pf_width / 3, self.contentView.pf_height - topMargin - bottomMargin);
    CGFloat titleLabelX = _newsImageView.pf_x + _newsImageView.pf_width + middleMargin;
    _titleLabel.frame = CGRectMake(titleLabelX, topMargin, self.contentView.pf_width - titleLabelX - rightMargin, titleLabelHeight);
    CGFloat detailLabelY = topMargin + _titleLabel.pf_height + 5;
    _detailLabel.frame = CGRectMake(titleLabelX, 5 + _titleLabel.pf_height + 5, _titleLabel.pf_width, self.contentView.pf_height - detailLabelY - 5);
    CGFloat numberCountsX = _detailLabel.pf_width * 3 / 5;
    CGFloat numberCountsY = _detailLabel.pf_height / 2;
    CGFloat numberCountsWidth = _detailLabel.pf_width * 2 / 5;
    CGFloat numberCountsHeight = _detailLabel.pf_height / 2;
    
//    1.判断父试图是不是_detailLabel
//    2.改变numberCountsLabel的父试图
//    if (_numberCountsLabel.superview == _detailLabel) {
    [_detailLabel addSubview:_numberCountsLabel];
    _numberCountsLabel.frame = CGRectMake(numberCountsX, numberCountsY, numberCountsWidth, numberCountsHeight);
//    视图坐标系转换
    CGRect newRect = [_numberCountsLabel.superview convertRect:_numberCountsLabel.frame toView:self.contentView];
    _numberCountsLabel.frame = newRect;
    [self.contentView addSubview:_numberCountsLabel];
//    }
}

- (void)setNewsObject:(NewsObject *)newsObject {
    _newsObject = newsObject;
    self.newsImageView.image = [UIImage imageNamed:newsObject.newsImage];
    self.titleLabel.text = newsObject.title;
    self.detailLabel.text = newsObject.newsDescription;
    self.numberCountsLabel.text = newsObject.newsNumberCount;
}


- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
